<template>
  <div class="settings-container">
    <header class="header">
      <h1>设置</h1>
      <button class="back-btn" @click="$router.go(-1)">
        <i class="fas fa-arrow-left"></i> 返回
      </button>
    </header>

    <main class="settings-content">
      <form @submit.prevent="handleSave">
        <div class="settings-section">
          <h2>服务器设置</h2>
          <div class="form-group">
            <label for="serverUrl">服务器地址</label>
            <input
              type="text"
              id="serverUrl"
              v-model="settings.serverUrl"
              required
              placeholder="ws://example.com:8000/ws"
            >
          </div>
        </div>

        <div class="settings-section">
          <h2>客户端设置</h2>
          <div class="form-group">
            <label for="clientName">客户端名称</label>
            <input
                type="text"
                id="clientName"
                v-model="settings.clientName"
                required
                placeholder="输入设备名称"
            >
          </div>

          <div class="form-group">
            <label>客户端ID</label>
            <input
                type="text"
                v-model="settings.clientId"
                readonly
                class="readonly-input"
            >
            <p class="form-hint">此ID用于标识当前设备，随机生成</p>
          </div>

          <div class="form-group">
            <label>客户端类型</label>
            <input
                type="text"
                v-model="settings.clientType"
                readonly
                class="readonly-input"
            >
          </div>
        </div>


        <div class="settings-section">
          <h2>提醒设置</h2>
          <div class="form-group checkbox-group">
            <input
              type="checkbox"
              id="alertSound"
              v-model="settings.alertSound"
            >
            <label for="alertSound">提醒时播放声音</label>
          </div>

          <div class="form-group checkbox-group">
            <input
              type="checkbox"
              id="alertVibration"
              v-model="settings.alertVibration"
            >
            <label for="alertVibration">提醒时震动（支持的设备）</label>
          </div>

          <div class="time-range-group">
            <h3>提醒时间范围</h3>
            <div class="form-group time-range">
              <div>
                <label for="startTime">开始时间</label>
                <input
                  type="time"
                  id="startTime"
                  v-model="settings.alertTimeRange.start"
                >
              </div>
              <span class="time-separator">-</span>
              <div>
                <label for="endTime">结束时间</label>
                <input
                  type="time"
                  id="endTime"
                  v-model="settings.alertTimeRange.end"
                >
              </div>
            </div>
            <p class="form-hint">仅在指定时间范围内才会显示提醒</p>
          </div>
        </div>

        <div class="form-actions">
          <button type="button" class="cancel-btn" @click="handleCancel">
            取消
          </button>
          <button type="submit" class="save-btn">
            保存设置
          </button>
        </div>
      </form>
    </main>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from 'vue'
import { useAppStore } from '../store'
import { useRouter } from 'vue-router'

const store = useAppStore()
const router = useRouter()

// 用于编辑的设置副本
const settings = reactive({
  serverUrl: '',
  alertSound: true,
  alertVibration: true,
  alertTimeRange: {
    start: '00:00',
    end: '23:59'
  }
})

// 保存原始设置用于取消操作
let originalSettings = null

// 加载设置
const loadSettings = () => {
  // 深拷贝设置
  originalSettings = JSON.parse(JSON.stringify(store.settings))
  Object.assign(settings, originalSettings)
}

// 处理保存设置
const handleSave = () => {
  store.saveSettings(settings)
  router.push('/')
}

// 处理取消
const handleCancel = () => {
  // 恢复原始设置
  Object.assign(settings, originalSettings)
  router.push('/')
}

// 组件挂载时加载设置
onMounted(() => {
  loadSettings()
})
</script>

<style scoped>
.settings-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  background-color: #f5f5f5;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem 2rem;
  background-color: #2c3e50;
  color: white;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.back-btn {
  background: none;
  border: none;
  color: white;
  cursor: pointer;
  font-size: 1rem;
  display: flex;
  align-items: center;
  gap: 8px;
}

.settings-content {
  flex: 1;
  padding: 2rem;
  display: flex;
  justify-content: center;
}

form {
  background-color: white;
  padding: 2rem;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  width: 100%;
  max-width: 800px;
}

.settings-section {
  margin-bottom: 2rem;
  padding-bottom: 1.5rem;
  border-bottom: 1px solid #eee;
}

.settings-section:last-child {
  border-bottom: none;
  margin-bottom: 0;
  padding-bottom: 0;
}

h2 {
  color: #2c3e50;
  margin-top: 0;
  margin-bottom: 1.5rem;
}

h3 {
  color: #34495e;
  margin-bottom: 1rem;
}

.form-group {
  margin-bottom: 1.5rem;
}

label {
  display: block;
  margin-bottom: 0.5rem;
  font-weight: 500;
  color: #2c3e50;
}

input {
  width: 100%;
  padding: 0.8rem;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 1rem;
}

.checkbox-group {
  display: flex;
  align-items: center;
}

.checkbox-group input {
  width: auto;
  margin-right: 0.5rem;
  margin-top: 0.2rem;
}

.checkbox-group label {
  margin-bottom: 0;
}

.time-range-group {
  margin-top: 1.5rem;
}

.time-range {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.time-range > div {
  flex: 1;
}

.time-separator {
  font-size: 1.5rem;
  color: #7f8c8d;
}

.form-hint {
  margin-top: 0.3rem;
  font-size: 0.8rem;
  color: #7f8c8d;
}

.form-actions {
  display: flex;
  justify-content: flex-end;
  gap: 1rem;
  margin-top: 2rem;
}

.cancel-btn, .save-btn {
  padding: 0.8rem 1.5rem;
  border-radius: 4px;
  font-size: 1rem;
  cursor: pointer;
}

.cancel-btn {
  background-color: #ecf0f1;
  color: #7f8c8d;
  border: none;
}

.cancel-btn:hover {
  background-color: #dcdde1;
}

.save-btn {
  background-color: #3498db;
  color: white;
  border: none;
}

.save-btn:hover {
  background-color: #2980b9;
}

.readonly-input {
  background-color: #f5f5f5;
  cursor: not-allowed;
}
</style>
